package com.cloud.bc.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloud.bc.common.core.domain.AjaxResult;
import com.cloud.bc.common.core.domain.TableDataInfo;
import com.cloud.bc.config.Config;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.service.BcUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
    @Autowired
    Config config;
    @Autowired
    BcUsersService bcUsersService;
    public ModelAndView baseView(HttpServletRequest request, String title, String viewPath) {
        return baseView(request, title, viewPath, false);
    }
    public ModelAndView baseView(
            HttpServletRequest request,
            String title, String viewPath, boolean isCommon) {
        boolean isMobile = false;
//        if (!isCommon) {
//            if (isMobile(request)) {
//                viewPath = "m/" + viewPath;
//                isMobile = true;
//            } else {
//                viewPath = "pc/" + viewPath;
//            }
//        }
        ModelAndView view = new ModelAndView(viewPath);
        view.addObject("title", title);
        view.addObject("webSite", config.getWebSite());
        view.addObject("staticVersion", config.getStaticVersion());
        view.addObject("defaultPageSize", config.getDefaultPageSize());
        view.addObject("staticSite", config.getStaticSite());
        view.addObject("imgUrl", config.getImgUrl());
        boolean isLogin = false;
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        if (null != userInfo && Convert.toInt(userInfo.getId(), 0) > 0) {
            isLogin = true;
            view.addObject("userInfo", userInfo);
        }
        view.addObject("isMobile", isMobile);
        view.addObject("isLogin", isLogin);
        return view;
    }
    public boolean isMobile(HttpServletRequest request) {
        boolean isMobile = false;
        //去获取是否为手机端
        String userAgent = request.getHeader("User-Agent");
        UserAgent ua = UserAgentUtil.parse(userAgent);
        //TODO 这里我认为如果是微信浏览器也是手机
        if (ua.isMobile() || userAgent.toLowerCase().contains("micromessenger")) {
            isMobile = true;
        }
        return isMobile;
    }
    public ModelAndView baseViewAjax(String title, String viewPath) {
        ModelAndView view = new ModelAndView(viewPath);
        view.addObject("webSite", config.getWebSite());
        view.addObject("staticVersion", config.getStaticVersion());
        view.addObject("staticSite", config.getStaticSite());
        view.addObject("imgUrl", config.getImgUrl());
        return view;
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(IPage pageData) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(1);
        rspData.setRows(pageData.getRecords());
        rspData.setTotal(pageData.getTotal());
        return rspData;
    }

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult toAjax(int rows) {
        return rows > 0 ? success() : error();
    }

    /**
     * 响应返回结果
     *
     * @param result 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(boolean result) {
        return result ? success() : error();
    }

    /**
     * 返回成功
     */
    public AjaxResult success() {
        return AjaxResult.success();
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error() {
        return AjaxResult.error();
    }

    /**
     * 返回成功消息
     */
    public AjaxResult success(String message) {
        return AjaxResult.success(message);
    }

    /**
     * 返回失败消息
     */
    public AjaxResult error(String message) {
        return AjaxResult.error(message);
    }

    /**
     * 返回错误码消息
     */
    public AjaxResult error(AjaxResult.Type type, String message) {
        return new AjaxResult(type, message);
    }

}
